/*     */ package com.chinapay.secss;
/*     */ 
/*     */ import java.math.BigInteger;
/*     */ import java.security.KeyFactory;
/*     */ import java.security.PrivateKey;
/*     */ import java.security.PublicKey;
/*     */ import java.security.Signature;
/*     */ import java.security.interfaces.RSAPrivateCrtKey;
/*     */ import java.security.interfaces.RSAPublicKey;
/*     */ import java.security.spec.PKCS8EncodedKeySpec;
/*     */ import java.security.spec.X509EncodedKeySpec;
/*     */ import org.bouncycastle.crypto.CipherParameters;
/*     */ import org.bouncycastle.crypto.engines.RSAEngine;
/*     */ import org.bouncycastle.crypto.params.RSAKeyParameters;
/*     */ import org.bouncycastle.crypto.params.RSAPrivateCrtKeyParameters;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class SecureUtil
/*     */ {
/*     */   public static byte[] sign(byte[] dataBytes, PrivateKey priKey, String sigAlgName) throws Exception {
/*  33 */     Signature signature = Signature.getInstance(sigAlgName);
/*  34 */     signature.initSign(priKey);
/*  35 */     signature.update(dataBytes);
/*  36 */     byte[] signByte = signature.sign();
/*     */     
/*  38 */     return signByte;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static boolean verify(byte[] dataBytes, byte[] signBytes, PublicKey pubKey, String sigAlgName) throws Exception {
/*  51 */     Signature signature = Signature.getInstance(sigAlgName);
/*  52 */     signature.initVerify(pubKey);
/*  53 */     signature.update(dataBytes);
/*     */     
/*  55 */     return signature.verify(signBytes);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] encryptData(byte[] dataBytes, byte[] keyBytes) throws Exception {
/*  66 */     return encryptByPublicKeyRSALong(dataBytes, keyBytes);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] decryptData(byte[] dataBytes, byte[] keyBytes) throws Exception {
/*  77 */     return decryptByPrivateKeyRSALong(dataBytes, keyBytes);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] pin2PinBlockWithCardNO(String aPin, String aCardNO) {
/*  86 */     byte[] tPinByte = pin2PinBlock(aPin);
/*  87 */     if (SecssUtil.isEmpty(tPinByte)) {
/*  88 */       return null;
/*     */     }
/*  90 */     if (aCardNO.length() == 11) {
/*  91 */       aCardNO = "00" + aCardNO;
/*     */     }
/*  93 */     else if (aCardNO.length() == 12) {
/*  94 */       aCardNO = "0" + aCardNO;
/*     */     } 
/*     */     
/*  97 */     byte[] tPanByte = formatPan(aCardNO);
/*  98 */     if (SecssUtil.isEmpty(tPanByte)) {
/*  99 */       return null;
/*     */     }
/* 101 */     byte[] tByte = new byte[8];
/* 102 */     for (int i = 0; i < 8; i++) {
/* 103 */       tByte[i] = (byte)(tPinByte[i] ^ tPanByte[i]);
/*     */     }
/* 105 */     return tByte;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] pin2PinBlock(String aPin) {
/* 113 */     int tTemp = 1;
/* 114 */     int tPinLen = aPin.length();
/*     */     
/* 116 */     byte[] tByte = new byte[8];
/*     */     try {
/* 118 */       tByte[0] = (byte)Integer.parseInt((
/* 119 */           new Integer(tPinLen)).toString(), 10);
/* 120 */       int i = 0;
/* 121 */       if (tPinLen % 2 == 0) {
/* 122 */         for (i = 0; i < tPinLen; ) {
/* 123 */           String a = aPin.substring(i, i + 2);
/* 124 */           tByte[tTemp] = (byte)Integer.parseInt(a, 16);
/* 125 */           if (i == tPinLen - 2 && tTemp < 7) {
/* 126 */             for (int x = tTemp + 1; x < 8; x++) {
/* 127 */               tByte[x] = -1;
/*     */             }
/*     */           }
/*     */           
/* 131 */           tTemp++;
/* 132 */           i += 2;
/*     */         } 
/*     */       } else {
/* 135 */         for (i = 0; i < tPinLen - 1; ) {
/* 136 */           String a = aPin.substring(i, i + 2);
/* 137 */           tByte[tTemp] = (byte)Integer.parseInt(a, 16);
/* 138 */           if (i == tPinLen - 3) {
/* 139 */             String b = 
/* 140 */               aPin.substring(tPinLen - 1) + 
/* 141 */               "F";
/* 142 */             tByte[tTemp + 1] = (byte)Integer.parseInt(b, 16);
/* 143 */             if (tTemp + 1 < 7) {
/* 144 */               for (int x = tTemp + 2; x < 8; x++) {
/* 145 */                 tByte[x] = -1;
/*     */               }
/*     */             }
/*     */           } 
/* 149 */           tTemp++;
/* 150 */           i += 2;
/*     */         } 
/*     */       } 
/* 153 */     } catch (Exception e) {
/* 154 */       return null;
/*     */     } 
/* 156 */     return tByte;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public static byte[] formatPan(String aPan) {
/* 164 */     int tPanLen = aPan.length();
/* 165 */     byte[] tByte = new byte[8];
/* 166 */     int temp = tPanLen - 13;
/*     */     try {
/* 168 */       tByte[0] = 0;
/* 169 */       tByte[1] = 0;
/* 170 */       for (int i = 2; i < 8; i++) {
/* 171 */         String a = aPan.substring(temp, temp + 2);
/* 172 */         tByte[i] = (byte)Integer.parseInt(a, 16);
/* 173 */         temp += 2;
/*     */       } 
/* 175 */     } catch (Exception e) {
/* 176 */       return null;
/*     */     } 
/* 178 */     return tByte;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static byte[] encryptByPublicKeyRSALong(byte[] data, byte[] keyBytes) throws Exception {
/*     */     try {
/* 194 */       X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(keyBytes);
/* 195 */       KeyFactory keyFactory = KeyFactory.getInstance(SecssConstants.ENC_ALG_PREFIX, SecssConstants.DEFAULT_PROVIDER);
/* 196 */       RSAPublicKey publicKey2 = (RSAPublicKey)keyFactory
/* 197 */         .generatePublic(x509KeySpec);
/*     */       
/* 199 */       BigInteger modulus = publicKey2.getModulus();
/* 200 */       BigInteger exponent = publicKey2.getPublicExponent();
/* 201 */       RSAKeyParameters pubKey = new RSAKeyParameters(false, modulus, 
/* 202 */           exponent);
/* 203 */       RSAEngine rSAEngine = new RSAEngine();
/* 204 */       rSAEngine.init(true, (CipherParameters)pubKey);
/*     */       
/* 206 */       int blockSize = rSAEngine.getInputBlockSize();
/* 207 */       int outputSize = rSAEngine.getOutputBlockSize();
/* 208 */       int leavedSize = data.length % blockSize;
/* 209 */       int blocksSize = (leavedSize != 0) ? (data.length / blockSize + 1) : (
/* 210 */         data.length / blockSize);
/* 211 */       byte[] raw = new byte[outputSize * blocksSize];
/* 212 */       int i = 0;
/* 213 */       byte[] encBytes = null;
/* 214 */       while (data.length - i * blockSize > 0) {
/* 215 */         if (data.length - i * blockSize > blockSize) {
/* 216 */           encBytes = rSAEngine.processBlock(data, i * blockSize, 
/* 217 */               blockSize);
/*     */         } else {
/* 219 */           encBytes = rSAEngine.processBlock(data, i * blockSize, 
/* 220 */               data.length - i * blockSize);
/*     */         } 
/* 222 */         System.arraycopy(encBytes, 0, raw, i * outputSize, outputSize);
/* 223 */         i++;
/*     */       } 
/* 225 */       return raw;
/* 226 */     } catch (Exception e) {
/* 227 */       throw e;
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   private static byte[] decryptByPrivateKeyRSALong(byte[] data, byte[] keyBytes) throws Exception {
/*     */     try {
/* 244 */       PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);
/* 245 */       KeyFactory keyFactory2 = KeyFactory.getInstance(SecssConstants.ENC_ALG_PREFIX, SecssConstants.DEFAULT_PROVIDER);
/* 246 */       RSAPrivateCrtKey privateKey2 = (RSAPrivateCrtKey)keyFactory2
/* 247 */         .generatePrivate(pkcs8KeySpec);
/*     */ 
/*     */       
/* 250 */       BigInteger mod = privateKey2.getModulus();
/* 251 */       BigInteger pubExp = privateKey2.getPublicExponent();
/* 252 */       BigInteger privExp = privateKey2.getPrivateExponent();
/* 253 */       BigInteger pExp = privateKey2.getPrimeExponentP();
/* 254 */       BigInteger qExp = privateKey2.getPrimeExponentQ();
/* 255 */       BigInteger p = privateKey2.getPrimeP();
/* 256 */       BigInteger q = privateKey2.getPrimeQ();
/* 257 */       BigInteger crtCoef = privateKey2.getCrtCoefficient();
/* 258 */       RSAPrivateCrtKeyParameters rSAPrivateCrtKeyParameters = new RSAPrivateCrtKeyParameters(
/* 259 */           mod, pubExp, privExp, p, q, pExp, qExp, crtCoef);
/* 260 */       RSAEngine rSAEngine = new RSAEngine();
/* 261 */       rSAEngine.init(false, (CipherParameters)rSAPrivateCrtKeyParameters);
/*     */       
/* 263 */       int blockSize = rSAEngine.getInputBlockSize();
/* 264 */       int outputSize = rSAEngine.getOutputBlockSize();
/* 265 */       int leavedSize = data.length % blockSize;
/* 266 */       int blocksSize = (leavedSize != 0) ? (data.length / blockSize + 1) : (
/* 267 */         data.length / blockSize);
/* 268 */       byte[] raw = new byte[outputSize * blocksSize];
/* 269 */       int i = 0;
/* 270 */       int count = 0;
/* 271 */       byte[] decData = null;
/* 272 */       while (data.length - i * blockSize > 0) {
/* 273 */         if (data.length - i * blockSize > blockSize) {
/* 274 */           decData = rSAEngine.processBlock(data, i * blockSize, blockSize);
/* 275 */           System.arraycopy(decData, 0, raw, i * outputSize, 
/* 276 */               outputSize);
/* 277 */           count += outputSize;
/*     */         } else {
/* 279 */           decData = rSAEngine.processBlock(data, i * blockSize, data.length - 
/* 280 */               i * blockSize);
/* 281 */           System.arraycopy(decData, 0, raw, i * outputSize, 
/* 282 */               decData.length);
/* 283 */           count += decData.length;
/*     */         } 
/* 285 */         i++;
/*     */       } 
/* 287 */       byte[] resultRaw = new byte[count];
/* 288 */       System.arraycopy(raw, 0, resultRaw, 0, count);
/* 289 */       return resultRaw;
/* 290 */     } catch (Exception e) {
/* 291 */       throw e;
/*     */     } 
/*     */   }
/*     */ }


/* Location:              C:\Users\Administrator\Downloads\jd-gui-windows-1.6.6\jd-gui-windows-1.6.6\chinapay-secure-1.5.1.jar!\com\chinapay\secss\SecureUtil.class
 * Java compiler version: 5 (49.0)
 * JD-Core Version:       1.1.3
 */